﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FrayTank1 : ControllerBase
{
    public override PlayerData GetPlayerData()
    {
        return new PlayerData()
        {
            Name = "谢尔曼M3",
            Author = "Fray",
            BaseImage = "Sprite/Base/Base07",
            GunImage = "Sprite/Gun/Gun05"
        };
    }

    private float _currentTimeAngle = 0;

    private float _speedAngle = 0;
    /// <summary>
    /// 当前旋转方向,正负时针
    /// </summary>
    private float _currentRotate = 1;

    private Vector3 _lastPosition = new Vector3();
    private float _lastAngle;
    private Vector2 _lastSpeed;

    private float _offsetAngle = 10;

    private float _distance;
    
    /// <summary>
    /// 刚开始正在寻找的状态
    /// </summary>
    private bool _finding = true;

    private bool _isScaned = false;

    public override IEnumerator LoopAction()
    {
        
        while (_finding)
        {
            //Debug.Log(Data.currentSpeed);
            //_speedAngle = Random.Range(0, 360);
            if (_distance > 5)
            {
                MoveTo(1, _lastPosition);
            }
            else
            {
                _speedAngle += 60;
                Move(1, _speedAngle, 1.5f);
            }     
            Turn(Time.realtimeSinceStartup * 100 * _currentRotate, _currentRotate * 100);
            _currentTimeAngle = Time.realtimeSinceStartup * 100;
            yield return new WaitForSeconds(1.5f);
        }
    
        }

    public override void Start()
    {
        base.Start();
        CoroutineMono.StartCoroutine(LoopAction());
        Talk("一战时德国最著名坦克，每秒射速三百五十发，穿甲爆破燃烧，瞬间完成", 3f);
    }

    private Coroutine coro = null;
    private bool test = false;

    public override void OnScanOthers(ScanOthersData scanOthersData)
    {
        Debug.Log("I see you");
        _lastPosition = scanOthersData.othersPosition;
        _distance = (Data.currentPosition - _lastPosition).magnitude;
        //Debug.Log("Distance=" + _distance);

        _lastAngle = scanOthersData.othersAngle;
        _lastSpeed = scanOthersData.othersSpeed;

        if (coro != null)
        {
            CoroutineMono.StopCoroutine(coro);
        }
        coro = CoroutineMono.StartCoroutine(OnScanMove(scanOthersData));
        
    }

    private IEnumerator OnScanMove(ScanOthersData scanOthersData)
    {
        //Debug.Log(Data.currentSpeed);
        _finding = false;  
        if (Data.currentEnergy >= 5)
        {
            if (_distance < 6&&_distance>1)
            {
                //int x = Random.Range(1, 5);
                //Fire(x, (int)(Data.currentEnergy / x));
                yield return new WaitForSeconds(0.02f);
                Fire(1, 5);
                Talk("打的不错", 2f);
            }
            else if (_distance >= 6f)
            {
                //int x = Random.Range(1, 5);
                //Fire(x, (int)(Data.currentEnergy / x));
                Fire(1, 2);
                Talk("打的不错", 2f);
            }
            else if (_distance < 1)
            {
                int x = Random.Range(1, 5);
                Fire(x, (int)(Data.currentEnergy / x));
            }

            if (!_isScaned)
            {
                if (_distance > 4)
                {
                    //_speedAngle = Data.currentAngle+;
                    //Move(1,_lastAngle, 2);
                    MoveTo(1, _lastPosition);
                }
                else if (_distance < 2.5f)
                {
                    _speedAngle = Data.currentAngle + 150;
                    Move(1, -_speedAngle, 2);

                }
                else if (_distance >= 2.5f && _distance <= 5)
                {
                    _speedAngle = Data.currentAngle + 90;
                    Move(1, -_speedAngle, 2);
                }
            }
        }
        Turn( _offsetAngle * 2 * _currentRotate, 80 * _currentRotate);
        yield return new WaitForSeconds(0.3f);
        Turn( - _offsetAngle * 2 *_currentRotate, -100*_currentRotate);
        _currentRotate *= -1;
        yield return new WaitForSeconds(0.3f);


        //Turn(Data.currentAngle + _offsetAngle * 3 * _currentRotate, +100 * _currentRotate);       
        //yield return new WaitForSeconds(1);

        //Turn(Data.currentAngle - _offsetAngle * 3 * _currentRotate, -100 * _currentRotate);
        //_currentRotate *= -1;
        //yield return new WaitForSeconds(1);

        _finding = true;
        CoroutineMono.StartCoroutine(LoopAction());
    }

    public override void OnScaned(float angle)
    {
        if (_isScaned)
        {
            return;
        }
        Talk("溜了溜了溜了", 1f);
        if (Random.Range(0, 1) > 0.5f) { 
            Stop();
            Move(1,_speedAngle+90, 1.5f);
        }
        else
        {
            Stop();
            Move(1, _speedAngle - 90, 1.5f);
        }
        _isScaned = true;
        CoroutineMono.StartCoroutine(change());
    }

    private IEnumerator change()
    {
        yield return new WaitForSeconds(1.5f);
        _isScaned = false;
        _finding = true;
        CoroutineMono.StartCoroutine(LoopAction());
    }

    public override void OnCrashWall()
    {
        MoveTo(1, Vector3.zero);
    }

    public override void OnCrashOthers(Vector3 othersPosition, float othersSpeed)
    {
        TurnTo(100, othersPosition);
    }

    public override void OnHitted(float bulletAngle)
    {
        Move(1, bulletAngle - 90, 2f);
        Talk("啊~不要~不要啊~", 1f);
    }
}